package view;

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import model.Chamado;
import control.ControllersDaos;

public class VChamado extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2214126331902325358L;
	private JPanel contentPane;
	private JTable table;
//	private ChamadoCRUD chamadoCrud = new ChamadoCRUD();
	private ControllersDaos controller;
	private Chamado chamadoIn = new Chamado();
	private VPrincipal telaAnt;
	private DefaultTableModel modelo ;
	

	/**
	 * Launch the application.
	 */
	/*
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					VChamado frame = new VChamado();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
*/
	/**
	 * Create the frame.
	 * @wbp.parser.constructor
	 */
	public VChamado(VPrincipal telaAnterior, ControllersDaos controlador) {
		this(controlador);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        telaAnt    = telaAnterior;
        
	}
	
	private VChamado(ControllersDaos controll) {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) {
				telaAnt.setVisible(true);
			}
		});
		controller = controll;
		setTitle("Chamados");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 600, 450);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		table = new JTable();
		table.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		table.setSurrendersFocusOnKeystroke(true);
		table.setToolTipText("");
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setRowSelectionAllowed(false);
		table.setOpaque(false);
		table.setAutoCreateRowSorter(false);  // não deixa ordenar
		table.setFont(new Font("Trebuchet MS", Font.BOLD, 12));
		table.setBorder(UIManager.getBorder("Table.scrollPaneBorder"));
		table.setBackground(Color.LIGHT_GRAY);
		table.setBounds(1, 26, 573, 64);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
			},
			new String[] {
				"N\u00BA Chamado", "Descri\u00E7\u00E3o", "Status"
			}
		) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			boolean[] columnEditables = new boolean[] {
				false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(1).setPreferredWidth(350);
		table.getColumnModel().getColumn(1).setMinWidth(75);
		table.getColumnModel().getColumn(2).setResizable(false);
		table.getColumnModel().getColumn(2).setMinWidth(30);
		table.getTableHeader().setReorderingAllowed(false); 
		
		
		modelo = (DefaultTableModel) table.getModel();
	    modelo.setNumRows(0);
		
		AtualizarTable();
		
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		scrollPane.setBounds(10, 22, 564, 315); // Define o tamanho do scroll, por padr�o coloca-se o mesmo tamanho do objeto adicionado ao mesmo.  
		//Panel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); // Adiciona barra de rolagem vertical
		contentPane.add(scrollPane); // Adicionel no meu Panel
		
		//contentPane.add(table);
		
		
		/*
		JButton btnAvaliar = new JButton("Avaliar");
		btnAvaliar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				if ((table.getSelectedRow() >=0)&&(table.getModel().getValueAt( table.getSelectedRow() ,2).toString().equalsIgnoreCase("Fechado"))){
					// pega o valor selecionado da primeira coluna
					int num_chamdo = Integer.parseInt((table.getModel().getValueAt( table.getSelectedRow() ,0)).toString());
					
					chamadoIn.setIdChamado(num_chamdo);
					
					VChamado.this.setVisible(false);
					VChamado2 telaChamado2 = new VChamado2(chamadoIn);
					telaChamado2.setVisible(true);
				}else{
					JOptionPane.showMessageDialog(null, "Selecione um chamado com status \"Aberto\"");
				}
			}
		});
		btnAvaliar.setFont(new Font("Trebuchet MS", Font.BOLD, 12));
		btnAvaliar.setBounds(10, 378, 89, 23);
		//contentPane.add(btnAvaliar); //por enquanto n�o vai ter a avalia��o.
		*/
		JButton btnFinalizar = new JButton("Finalizar");
		btnFinalizar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
					// verificar se est� selecionado alguma linha e se a linha selecionada est� com status Aberto
				if ((table.getSelectedRow() >=0)&&(table.getModel().getValueAt( table.getSelectedRow() ,2).toString().equalsIgnoreCase("Aberto"))){
					// pega o valor selecionado da primeira coluna
					int num_chamdo = Integer.parseInt((table.getModel().getValueAt( table.getSelectedRow() ,0)).toString());
					
					chamadoIn.setIdChamado(num_chamdo);
					
					
					VChamado.this.setVisible(false);
					VChamado2 telaChamado2 = new VChamado2(VChamado.this,chamadoIn,controller);
					telaChamado2.setVisible(true);
				}else{
					JOptionPane.showMessageDialog(null, "Selecione um chamado com status \"Aberto\"");
				}
				
			}
		});
		btnFinalizar.setFont(new Font("Trebuchet MS", Font.BOLD, 12));
		btnFinalizar.setBounds(485, 378, 89, 23);
		contentPane.add(btnFinalizar);
		
		
	}
	
	public void AtualizarTable(){
		modelo.setNumRows(0);
		try{
	        //chamadoCrud.listChamado(modelo);
			controller.listChamado(modelo);
		} catch (ClassNotFoundException ex) {
			JOptionPane.showMessageDialog(null, "Problema no Preenchimento da tabela de chamados: " 
					+ ex.getMessage());
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(null,"Problema no Preenchimento da tabela de chamados: "
					+ ex.getMessage());
		}
	}
}
